package com.sky.sky_server.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.result.Result;
import com.sky.sky_pojo.dto.DishDTO;
import com.sky.sky_pojo.entity.DishQueryPage;
import com.sky.sky_server.service.impl.DishServiceImpl;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequestMapping("/admin/dish")
@RestController("adminDishController")
public class DishController {

    @Autowired
    private DishServiceImpl dishServiceImpl;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品: {}", dishDTO);
        int res = dishServiceImpl.addDishAndFlavor(dishDTO);
        if(res > 0){
            // 清理菜品的缓存数据，当前菜品所属分类
            String key = "dish_" + dishDTO.getCategoryId().toString();
            redisTemplate.delete(key);
        }
        return res > 0 ? Result.success("新增菜品成功") : Result.error("新增菜品失败");
    }

    /**
     *  根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam String categoryId) {
        log.info("根据分类id查询菜品 分类id {}", categoryId);
        Long categoryIdLong = Long.parseLong(categoryId);
        return Result.success(dishServiceImpl.listByCategoryId(categoryIdLong));
    }

    /**
     * 菜品的分页查询
     * @param dishQueryPage
     * @return
     */
    @GetMapping("/page")
    public Result page(@Valid DishQueryPage dishQueryPage){
        log.info("菜品分页查询，dishQueryPage:{}", dishQueryPage);
        return Result.success(dishServiceImpl.page(dishQueryPage));
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除菜品，ids:{}", ids);
        int res = dishServiceImpl.deleteDishByIds(ids);
        if(res == -1)
            return Result.error("菜品正在被使用，无法删除");

        // 清理缓存，不用单独查询删除的这些菜品所属的菜品分类，直接删除所有缓存
        deleteCache("dish_*");

        return Result.success("删除成功");
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, @RequestParam Long id) {
        log.info("修改菜品状态，status = {}, id = {}", status, id);
        int res = dishServiceImpl.updateStatus(id, status);
        if(res > 0){
            // 清理缓存，不用单独查询删除的这些菜品所属的菜品分类，直接删除所有缓存
            deleteCache("dish_*");
        }
        return res > 0 ? Result.success("修改菜品状态成功") : Result.error("修改菜品状态失败");
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("根据id查询菜品，id = {}", id);
        return Result.success(dishServiceImpl.getById(id));
    }

    /**
     * 修改菜品
     * @param dish
     * @return
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dish) {
        log.info("修改菜品，dish = {}", dish);
        int res = dishServiceImpl.update(dish);
        if(res > 0){
            // 清理缓存，不用单独查询删除的这些菜品所属的菜品分类，直接删除所有缓存
            deleteCache("dish_*");
        }
        return res > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    private void deleteCache(String pattern) {
        var keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
